package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.TbUserTenementInfo;
import com.thinkgem.jeesite.modules.sys.service.TenementPayInfoService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by loutong on 2016/11/2.
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tenementPayInfoController")
public class TenementPayInfoController extends BaseController {

    @Autowired
    private TenementPayInfoService tenementPayInfoService;
    //代理商查询他所代理区域的物业缴费记录
    @RequestMapping(value="queryByUid")
    public String queryByUid(TbUserTenementInfo tbUserTenementInfo, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes){

        String uid =UserUtils.getUser().getId();
        tbUserTenementInfo.setUid(uid);
        Page<TbUserTenementInfo> page=tenementPayInfoService.findByOrderUid(new Page<TbUserTenementInfo>(request, response),tbUserTenementInfo);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
            addMessage(redirectAttributes, "无查询结果");
        }
        model.addAttribute("page", page);
        List tlist=tenementPayInfoService.getListMoney(tbUserTenementInfo);
        model.addAttribute("one",tlist.get(0));
        model.addAttribute("two",tlist.get(1));
        model.addAttribute("three",tlist.get(2));
        model.addAttribute("six",tlist.get(3));
        model.addAttribute("maxMoney",tlist.get(4));
        if(null==tbUserTenementInfo||(null!=tbUserTenementInfo&&null==tbUserTenementInfo.getBeginDate())){
            Date date=new Date();
            tbUserTenementInfo.setBeginDate(date);
            tbUserTenementInfo.setEndDate(date);
        }
        return "modules/sys/tenementPayInfoList";
    }
    //结算历史的饼图的的数据读取
    @RequestMapping("/getHistroyPicture")
    public void getHistroyPicture(HttpServletRequest request, HttpServletResponse response, Model model, String goodsNum) throws JSONException, IOException {

        JSONArray arr = new JSONArray();
       /* List tlist=tenementPayInfoService.getListMoney();

        JSONObject json = new JSONObject();
        json.put("one",tlist.get(0));
        json.put("two", tlist.get(1));
        json.put("three",tlist.get(2));
        json.put("six",tlist.get(3));
        json.put("maxMony",tlist.get(4));
        arr.put(json);*/
        if(null!=arr){
            response.getWriter().print(arr);
        }

    }
    //communityFee，物业管理员查询他所在小区的物业费用
    @RequestMapping(value="communityTenementFee")
    public String communityTenementPay(TbUserTenementInfo tbUserTenementInfo, HttpServletRequest request, HttpServletResponse response, Model model){
        Page<TbUserTenementInfo> page=tenementPayInfoService.communityFee(new Page<TbUserTenementInfo>(request, response),tbUserTenementInfo);
        model.addAttribute("page", page);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
        }
        List tlist=tenementPayInfoService.getCommunityListMoney(tbUserTenementInfo);
        model.addAttribute("one",tlist.get(0));
        model.addAttribute("two",tlist.get(1));
        model.addAttribute("three",tlist.get(2));
        model.addAttribute("six",tlist.get(3));
        model.addAttribute("maxMoney",tlist.get(4));
        if(null==tbUserTenementInfo||(null!=tbUserTenementInfo&&null==tbUserTenementInfo.getBeginDate())){
            Date date=new Date();
            tbUserTenementInfo.setBeginDate(date);
            tbUserTenementInfo.setEndDate(date);
        }
        return "modules/sys/communityTenementPayInfoList";
    }
}
